//
//  BaseModel.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/1.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class BaseModel: NSObject {
    
    var result = false
    var msg = ""
    var data : Any?
    init(array : Array<Dictionary<String,Any>>) {
        super.init()
    }
    
    init(source sourceDt : Dictionary<String,Any>?){
        super.init()
        guard sourceDt != nil else {
            return
        }
        
        if sourceDt!.keys.contains("result") {
            result = sourceDt!["result"] as! Bool
        }
        
        if sourceDt!.keys.contains("msg") {
            msg = sourceDt!["msg"] as! String
        }
        if sourceDt!.keys.contains("data") {
            data = sourceDt!["data"]
        }
        
        if sourceDt!.keys.contains("code") {
            if result == false {
                if let cod = sourceDt!["code"] as? Int {
                    if cod == 1 {
                        NotificationCenter.default.post(name: NSNotification.Name("needLogin"), object: NSNumber.init(value: 1))
                        return
                    }//请先登录
                    if cod == 2 {
                        NotificationCenter.default.post(name: NSNotification.Name("needLogin"), object: NSNumber.init(value: 2))
                        return
                    }//token过期
                }
            }
        }
    }
    
    override func setValue(_ value: Any?, forKey key: String) {
        super.setValue(value, forKey: key)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
        print("undefined key was set!")
    }
}
